$(function () {







  const admit = localStorage.getItem('someone');
  const m_goods = $('.m_goods');
  const myCart = $('.m-cart');
  const hello = $('.hello');
  const Admit = $('.navAdmit');
  const Reg = $('.navReg');
  // 1.1获取谁在登录
  // admit ? hello.html(admit) : hello.html('考拉欢迎你');
  // 1.3处理登录后事件(显示登录名等)
  if (admit) {
    hello.html(admit);
    Admit.hide();
    Reg.hide();
  }

  if (admit) {
    console.log(admit);
    show_cart(
      { username: admit }
    ).then(res => {
      // console.log(res);
      const { status, data } = res;
      // console.log(data);
      if (status) {
        let html = '';
        data.forEach(v => {
          const { goods_id, cart_id, goods_des, goods_package, goods_img, goods_price, num } = v;
          html += `
            <li>
            <div class="row0">
              <input type="checkbox" class="check-one" data-id="${cart_id}">
            </div>
            <div class="row1">
              <img
                src="${goods_img}"
                alt="">
              <div class="txt">
              ${goods_des}
              </div>
              <div class="package">
                 套餐:标配(20w充电套餐)
              </div>
            </div>
            <div class="row2">
             ${goods_price}
            </div>
            <div class="row3">
              <button class="reduce"  data-id="${goods_id}">-</button>
              <input type="text"  class="count-input" value="${num}"  disabled >
              <button class="add"  data-id="${v.goods_id}">+</button>
            </div>
            <div class="row4">
              ${num * goods_price}
            </div>
            <div class="row5">
              <span class="delete"  data-id="${cart_id}">删除</span>
            </div>
          </li>
            `
        });

        m_goods.html(html);
        addClass()

      } else {
        // alert('购物车空空如也');
        // location.href = '../html/4.goodlist.html';
      }

    })
  } else {
    alert('请先登录');
    location.href = '../html/3.login.html';
  }

  const selectAll = $('.selectAll')
  // 1.事件委托
  myCart.on('click', function (e) {

    // 2.全选
    if ($(e.target).hasClass('selectAll')) {
      let oneS = $('.check-one');
      if ($(e.target).prop('checked')) {
        oneS = $('.check-one');
        oneS.prop('checked', true);
      } else {
        oneS.prop('checked', false);
      }
      sumOne();
      countOne();
      return;

    }

    // 3.反选
    if ($(e.target).hasClass('check-one')) {
      sumOne();
      countOne();
      for (var i = 0; i < $('.check-one').length; i++) {
        if (!$('.check-one').eq(i).prop('checked')) {
          break;
        }
      }
      if (i == $('.check-one').length) {
        selectAll.prop('checked', true);
      } else {
        selectAll.prop('checked', false);
      }
      return;
    }

    // 4.删除
    if ($(e.target).hasClass('delete')) {
      let id = $('.delete').attr('data-id');
      // console.log(id);
      del({
        id
      }).then(res => {
        console.log(res);
        const { status, msg } = res;
        if (status) {
          $(e.target).parents('li').remove();
          countOne();
          sumOne();

          //删除时判断 是否还有孩子  没有孩子  改变全选按钮的状态
          if ($('.m_goods').children().length == 0) {
            selectAll.prop('checked', false);

            $('.cart').hide();
            $('.empty').show();

          }
          for (var i = 0; i < $('.check-one').length; i++) {
            selectAll.prop('checked', $('.check-one').eq(i).prop('checked'));
          }
        } else {
          alert(msg);
        }
      })
    }


    // 5.加
    if ($(e.target).hasClass('add')) {
      let id = $('.add').attr('data-id');
      shopping({
        username: admit,
        num: 1,
        id: id
      }).then(res => {
        // console.log(res);
        const { status, msg } = res;
        if (status) {
          let values = $(e.target).prev().val();
          values++;
          $(e.target).prev().val(values);
          //数量加以后去掉原来上面的off
          $(e.target).prev().prev().removeClass('off');

          //小计改变
          $(e.target).parent().siblings('.row4').html($(e.target).prev().val() * $(e.target).parent().siblings('.row2').html());

          sumOne();
          countOne();
        } else {
          alert(msg);
        }
      })
    }

    //6.减
    if ($(e.target).hasClass('reduce')) {
      // 先判断
      if ($(e.target).hasClass('off')) {
        return;
      }

      let id = $('.add').attr('data-id');
      shopping({
        username: admit,
        num: -1,
        id: id
      }).then(res => {
        // console.log(res);
        const { status, msg } = res;
        if (status) {
          let values = $(e.target).next().val();
          if (values > 1) {
            values--;
            $(e.target).next().val(values);
            $(e.target).parent().siblings('.row4').html($(e.target).next().val() * $(e.target).parent().siblings('.row2').html());
          }
          if ($(e.target).next().val() == 1) {
            $(e.target).addClass('off');
          }
          sumOne();
          countOne();
        } else {
          alert(msg);
        }
      })
    }

    // 7.全部删除
    if ($(e.target).hasClass('deleteAll')) {
      let arr = [];
      var ids = '';
      for (let j = 0; j < $('.check-one').length; j++) {
        if ($('.check-one').eq(j).prop('checked')) {
          arr.push($('.check-one').eq(j).attr('data-id'))
          ids += $('.check-one').eq(j).attr('data-id') + ','
        }
      }
      ids = ids.substring(0, ids.length - 1);
      delAll({
        id: ids,
      }).then(res => {
        console.log(res);
        const { status, msg } = res;
        if (status) {
          if ($('.selectAll').prop('checked')) {
            m_goods.remove();
            $('.selectAll').prop('checked', false)
            $('.cart').hide();
            $('.empty').show();

            countOne();
            sumOne();
          } else {
            for (let j = 0; j < $('.check-one').length; j++) {
              console.log(11);
              if ($('.check-one').eq(j).prop('checked')) {
                console.log(j);
                $('.check-one').eq(j).parents('li').remove();
                j--;
              }
              countOne();
              sumOne();
            }
          }
        } else {
          alert(msg);
        }
      })
    }

    // 8.结算
    if ($(e.target).hasClass('gobuy')) {
      // alert('你一共消费' + $('.money').html() + '元');

      localStorage.setItem('money', $('.money').html())
      let arr = [];
      var ids = '';
      for (let j = 0; j < $('.check-one').length; j++) {
        if ($('.check-one').eq(j).prop('checked')) {
          arr.push($('.check-one').eq(j).attr('data-id'))
          ids += $('.check-one').eq(j).attr('data-id') + ','
        }
      }
      ids = ids.substring(0, ids.length - 1);
      if (ids) {
        delAll({
          id: ids,
        }).then(res => {
          console.log(res);
          const { status, msg } = res;
          if (status) {
            if ($('.selectAll').prop('checked')) {
              m_goods.remove();
              $('.selectAll').prop('checked', false);
              $('.cart').hide();
              $('.empty').show();
              countOne();
              sumOne();
              location.href = '../html/8.order.html';
            } else {
              for (let j = 0; j < $('.check-one').length; j++) {
                console.log(11);
                if ($('.check-one').eq(j).prop('checked')) {
                  console.log(j);
                  $('.check-one').eq(j).parents('li').remove();
                  j--;
                }
                countOne();
                sumOne();
              }
            }
          } else {
            alert(msg);
          }
        })

      }
    }





  })

  //  数量总计
  function sumOne() {
    console.log(666);
    let sum = 0;
    for (var i = 0; i < $('.check-one').length; i++) {
      // 判断是否选中
      if ($('.check-one').eq(i).prop('checked')) {
        sum += $('.check-one').eq(i).parent().siblings('.row3').children('.count-input').val() * 1;
      }
    }
    $('.nums').html(sum);
  }


  //总价合计
  function countOne() {
    let sumS = 0;
    for (var i = 0; i < $('.check-one').length; i++) {
      // 判断是否选中
      if ($('.check-one').eq(i).prop('checked')) {
        sumS += $('.check-one').eq(i).parent().siblings('.row4').html() * 1;

      }
    }
    let sumNew = sumS.toFixed(2)
    $('.money').html(sumNew);
  }


  // 用来减号的判断
  function addClass() {
    for (let i = 0; i < $('.count-input').length; i++) {
      if ($('.count-input').eq(i).val() == 1) {
        $('.count-input').eq(i).prev().addClass('off');
      }
    }
  }
})